﻿@model VaccineEbook.Models.CustomPageList<VaccineEbook.Models.DBEntity.Vaccine>
@{
    ViewBag.Title = "Vaccine";
}

<h2>Vaccine</h2>

<!--Edit-->
<form class="form-inline">
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#edit-vaccine-modal" onclick="Show_AddVaccineModal()">
        Add Vaccine
    </button>
    
    <input type="text" class="form-control" placeholder="Enter text"
           name="@VaccineEbook.Models.Constant.QUICK_SEARCH_STRING"
           value="@Request.QueryString[VaccineEbook.Models.Constant.QUICK_SEARCH_STRING]" />
    <button type="submit" class="btn btn-primary">Search</button>
</form>

@Html.ValidationSummary(true)

<!--Display-->
<nav>
    @Html._PageListPager(Request.QueryString, Model, VaccineEbook.Models.Constant.PAGE_LIST_OPTION, null)    
</nav>

<table class="table table-bordered table-striped">
    <thead>
        <tr>
            <th>Edit</th>
            <th>Vaccine Name</th>
            <th>Create Date</th>
        </tr>
    </thead>
    <tbody>
        @if (Model.ListCustom != null) { 
            foreach (var item in Model.ListCustom) {
                <tr>
                    <td>
                        <a href="#" onclick="Show_EditVaccineModal(@item.VaccineID)">Edit</a>
                        |
                        <a href="#" onclick="Show_DeleteVaccineModal('@item.VaccineID', '@item.VaccineName')" >Delete</a>
                    </td>
                    <td>@item.VaccineName</td>
                    <td>@item.CreateDate</td>
                </tr>
            }
        }
    </tbody>
</table>

<nav>
    @Html._PageListPager(Request.QueryString, Model, VaccineEbook.Models.Constant.PAGE_LIST_OPTION, null)
</nav>

<!--Edit model-->
<div class="modal fade" id="edit-vaccine-modal">
    <div id="edit-vaccine-container"></div>
</div>

@section script{
<script type="text/javascript">

    function Show_AddVaccineModal() {
        var url = '@Url.Action("EditVaccine")'; // the url to the controller
        $.get(
            url,
            function (data) {
            $('#edit-vaccine-container').html(data);
            $('#edit-vaccine-modal').modal('show');
        });
    }

    function Show_EditVaccineModal(vaccineID) {
        var url = '@Url.Action("EditVaccine")' + "?VaccineId=" + vaccineID; // the url to the controller
        $.get(
            url,
            function (data) {
                $('#edit-vaccine-container').html(data);
                $('#edit-vaccine-modal').modal('show');
            });
    }

    function Show_DeleteVaccineModal(vaccineID, vaccineName) {
        if (confirm("Delete vaccine " + vaccineName)) {
            // make post form
            var url = '@Url.Action("DeleteVaccine")' + "?VaccineId=" + vaccineID;
            post(url, { id: vaccineID });
        }
    }

    function post(path, params, method) {
        method = method || "post"; // Set method to post by default if not specified.

        // The rest of this code assumes you are not using a library.
        // It can be made less wordy if you use one.
        var form = document.createElement("form");
        form.setAttribute("method", method);
        form.setAttribute("action", path);

        for (var key in params) {
            if (params.hasOwnProperty(key)) {
                var hiddenField = document.createElement("input");
                hiddenField.setAttribute("type", "hidden");
                hiddenField.setAttribute("name", key);
                hiddenField.setAttribute("value", params[key]);

                form.appendChild(hiddenField);
            }
        }

        document.body.appendChild(form);
        form.submit();
    }
</script>
}